//
//  Status.swift
//  Weibo T
//
//  Created by mac on 15/12/1.
//  Copyright © 2015年 陈肖坤. All rights reserved.
//

import UIKit

class Status: NSObject {
    ///微博创建时间
    var created_at: String?
    //微博ID
    var id: Int = 0
    //微博正文
    var text: String?
    //微博来源
    var source: String?
    //添加user属性
    var user: User?
    
    //被转发的微博的字段
    var retweeted_status: Status?
    //配图数组属性
    var pic_urls: [[String : String]]? {
        didSet {
            //给 imageURLs赋值
            guard let urls = pic_urls else {
                //终止set操作
                return
            }
            //实例化 imageURLs
            imageURLs = [NSURL]()
            
            //遍历urls
            for item in urls {
                let urlString = item["thumbnail_pic"]
                //相信后端给的urlString一定能够转换成url
                let largeString = urlString?.stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")
                let url = NSURL(string: largeString!)!
                imageURLs?.append(url)

            }
        }
    }
    
    var imageURLs: [NSURL]?
    

    //构造方法  kvc设置初始值
    init(dict: [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        
        //判断user键 对应的value  能否转化为 User(dict  对应的字典
        if key == "user" {
            if let dict = value as? [String : AnyObject]  {
                //字典转模型
                user = User(dict: dict)
            }
            return
        }
        //判断user键 对应的value  能否转化为 User(dict  对应的字典
        if key == "retweeted_status" {
            if let dict = value as? [String : AnyObject]  {
                //字典转模型
                retweeted_status = Status(dict: dict)
            }
            return
        }

        
        //一定要注意 需要调用super
        super.setValue(value, forKey: key)
    }
    
    //过滤
    override func setValue(value: AnyObject?, forUndefinedKey key: String) { }
    
    
    //重写description 属性
    override var description: String {
        let keys = ["created_at","id","text","source"]
        return dictionaryWithValuesForKeys(keys).description
    }
}
